app.controller('SaleShiftCtrl', function ($scope, ajax, $modal, toaster ,$stateParams,$filter,$rootScope,$timeout) {
	
    $scope.salesId=$stateParams.saleId;
    
    $scope.subject=$stateParams.subject;
	
    //起始客运站列表读取
    (function() {
        ajax.call(new Req.GET({
            resource: 'starting/bk/list'
        })).then(function(result) {
            $scope.starList = result.result;
        }, function() { });
    })();
    

    $scope.reload = $scope.page.loader('sales/bk/salesShift/{salesId}/{pageIndex}/{pageSize}/page'.format({salesId:$scope.salesId}));
    $scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
    $scope.reload(1);
    
    //模糊查询
    $scope.queryMap = {};
	$scope.timeDeparture = {};
    $scope.query = function () { 
        if ($scope.starList.selected != undefined) {
        	$scope.queryMap.stationStart__id= $scope.starList.selected.id;
        } else if ($scope.starList.selected == undefined) {
            delete $scope.queryMap.stationStart__id;
        }
        if ($scope.timeDeparture.startTime) {
            var dStartTime = $filter("date")($scope.timeDeparture.startTime, "yyyy-MM-dd");
            var dEndTime = $filter("date")($scope.timeDeparture.startTime, "yyyy-MM-dd");
            $scope.queryMap.departureDate=dStartTime + "#@" + dEndTime;
        } else if ($scope.timeDeparture.startTime == null) {
            delete  $scope.queryMap.departureDate;
        }
        $scope.reload(1, $scope.queryMap);
    };
   

	var inserter = new Req.POST({ resource: $scope.resource });

    // 添加
	$scope.add = function () {
		var modalInstance = $modal.open({
            templateUrl: 'template/public/sales/addSalesShift.html?v=353b1f4000',
            controller: 'SalesShiftAddCtrl',
            size: 'lg',
            resolve: {
                param: function () {
                    return {
                        "salesId": $scope.salesId
                    };
                }
            }
        });
		modalInstance.result.then(function (param) {
			inserter.datalist = param.ids;
			inserter.resource = 'sales/bk/'+$scope.salesId+'/'+param.startId+'/salesShift';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '关联班次成功。');
					$scope.reload(1);
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '关联班次失败。');
			});
		});
    };
    
	var deleter = new Req.DELETE({ resource: $scope.resource });
    
	//删除
	$scope.del = function (size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
		modalInstance.result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'sales/bk/salesShift/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload);
			}
		});
	};
	
});
;



